#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <sys/msg.h>
#include "../Headers/msgqueue.h"

struct q_entry {
    long mpriority;
    char text[MAX_MSG_SIZE];
}q_entry;

int
NewQueue(key_t key, int flags)
{
	return msgget(key, flags);
}

int
SendMsgQueue(int mqid, void * msg, int flags)
{
	struct q_entry * ptr;
	
	ptr = msg;
	
	int len;
	len = strlen(ptr->text);
	
	return msgsnd(mqid, msg, len + 1, flags);
}

int
GetMsgQueue(int mqid, void * msg, long msg_type)
{
	return msgrcv(mqid, msg, MAX_MSG_SIZE, msg_type, MSG_NOERROR | IPC_NOWAIT);
}

int
GetMsgQueueCli(int mqid, void * msg, long msg_type)
{
	return msgrcv(mqid, msg, MAX_MSG_SIZE, msg_type, MSG_NOERROR);
}

int
CloseQueue(int mqid)
{
	return msgctl(mqid, IPC_RMID, NULL);
}

